import { getSongUrl, getLyric } from '../../api/services/api'
import { getUserDetail } from '../../api/services/user'
import { parsingLyric } from '../../utils/common';
export default {
    namespaced: true,
    state: {
        currentMusic: {
            play: false,
            id: '',
            name: '',
            artistName: [
                {
                    name: '还没有'
                }
            ],
            img: '',
            url: '',
            lyric: [],
            time: 1,
            currentPlayTime: 0,
            //控制导航菜单的选择效果
            activeIndex: "/home/index",
        },
        tempMusicList: [],
        //当前音乐播放模式 默认为1，全部循环，2：单曲循环，3：随机播放。
        currentPlayMode: 1,
    },
    mutations: {
        //进入页面的时候，根据本地存储的音乐改变播放列表
        changeTempMusicList(state, val) {
            let arr = JSON.parse(localStorage.getItem('temp_MusicList'));
            state.tempMusicList = arr;
        },
        //把用户点击播放的音乐加入到本地存储
        addLoaclStorage(state, val) {
            //添加一个新的歌曲对象加入本地存储，用来存储历史播放记录
            const obj = {
                id: val.id,
                name: val.name,
                img: val.al.picUrl,
                artistName: val.ar,
                time: parseInt(val.dt / 1000),
                endtime: val.dt,
            };
            let lishi = localStorage.getItem("temp_MusicList");
            if (lishi) {
                lishi = JSON.parse(lishi);
                console.log('历史数据哦哦哦', lishi);
                let chong = lishi.some(elem => elem.id == obj.id);
                if (!chong) {
                    lishi.push(obj);
                    localStorage.setItem("temp_MusicList", JSON.stringify(lishi));
                }
            } else {
                let arr = [obj];
                localStorage.setItem("temp_MusicList", JSON.stringify(arr));
            }
        },
        //改变当前播放歌曲的url
        changeCurrentMusicUrl(state, val) {
            state.currentMusic.url = val.url;
            state.currentMusic.id = val.id;
        },
        changCurrentPlayStu(state, val) {
            state.currentMusic.play = val;
        },
        //改变当前播放歌曲的图片和名字-------首页
        changeCurrentMusicName(state, val) {
            state.currentMusic.play = true;
            state.currentMusic.img = val.album.blurPicUrl;
            state.currentMusic.name = val.album.name;
            state.currentMusic.artistName = val.album.artists;
            console.log(state.currentMusic);
        },
        //搜索页面方法---------------------------播放方法
        searchChangeCurrentMusicName(state, val) {
            state.currentMusic.play = true;
            state.currentMusic.img = val.al.picUrl;
            state.currentMusic.name = val.name;
            state.currentMusic.artistName = val.ar[0].name;
            state.currentMusic.time = parseInt(val.dt / 1000);
        },
        //改变歌单页面当前播放歌曲的图片和名字
        copyChangeCurrentMusicName(state, val) {
            state.currentMusic.play = true;
            state.currentMusic.img = val.al.picUrl;
            state.currentMusic.name = val.name;
            state.currentMusic.time = parseInt(val.dt / 1000);
            state.currentMusic.artistName = val.ar;
            console.log(state.currentMusic);
        },
        //改变当前播放音乐的歌词
        changCurrentMusicLyric(state, val) {
            const wodeci = parsingLyric(val);
            state.currentMusic.lyric = wodeci;
        },
        //改变当前时间
        changeCurrentTime(state, val) {
            state.currentMusic.currentPlayTime = val;
            // currentPlayTime
        },

        //点击下一首或者上一首后改变当前播放的歌曲
        nextChangCurrentMusic(state, val) {
            //将当前播放的音乐时间归零
            state.currentMusic.currentPlayTime = 0;
            console.log('当前播放音乐时间清零拉', state.currentMusic.currentPlayTime);
            state.currentMusic = { ...state.currentMusic, ...state.tempMusicList[val] };
        },
        //临时播放音乐列表点击
        listChangeCurrentMusic(state, val) {
            state.currentMusic.currentPlayTime = 0;
            state.currentMusic = { ...state.currentMusic, ...val, };
            console.log(state.currentMusic);
        },

        //切换音乐循环模式
        changePlayModeType(state, val) {
            if (state.currentPlayMode < 3) {
                state.currentPlayMode++;
            } else {
                state.currentPlayMode = 1;
            }
        }
    },
    actions: {

        //获取歌曲url getSongUrl
        async getSongUrl(context, val) {
            const data = await getSongUrl(val);
            console.log('音乐url', data);
            context.commit('changeCurrentMusicUrl', data.data[0]);
            context.commit('changCurrentPlayStu', true);
            await context.dispatch('getLyric', val);
            return data;
        },
        //获取歌词
        async getLyric(context, val) {
            const data = await getLyric(val);
            console.log('歌词', data);
            context.commit('changCurrentMusicLyric', data.lrc.lyric)
            return data;
        },
        //上一首歌，下一首歌
        async nextSong(context, val) {
            const { tempMusicList, currentMusic } = context.state;
            let muqian = 0;
            console.log('历史播放和当前', tempMusicList, currentMusic);

            //找到当前音乐在临时播放列表的位置
            tempMusicList.forEach((elem, index) => {
                if (elem.id == currentMusic.id) {
                    muqian = index;
                    return
                }
            });
            console.log('目前音乐下标', muqian);
            //判断val是true还是false，true则执行下一首，false则执行上一首
            if (val) {
                //如果当前播放的音乐是历史播放列表的最后一首，下一首则播放第一首
                if (muqian == tempMusicList.length - 1) {
                    context.commit('nextChangCurrentMusic', 0);
                    context.dispatch('getSongUrl', tempMusicList[0].id);
                }
                //否则播放下一首
                else {
                    context.commit('nextChangCurrentMusic', muqian + 1);
                    context.dispatch('getSongUrl', tempMusicList[muqian + 1].id);
                }
            } else {
                //上一首歌的操作
                //如果已经是第一首则播放最后一首
                if (muqian == 0) {
                    context.commit('nextChangCurrentMusic', tempMusicList.length - 1);
                    context.dispatch('getSongUrl', tempMusicList[tempMusicList.length - 1].id);

                } else {
                    context.commit('nextChangCurrentMusic', muqian - 1);
                    context.dispatch('getSongUrl', tempMusicList[muqian - 1].id);
                }
            }
        },
        //随机循环模式
        async randomCycle(context, val) {
            const { tempMusicList, currentMusic } = context.state;
            let muqian = 0;
            console.log('历史播放和当前', tempMusicList, currentMusic);
            //找到当前音乐在临时播放列表的位置
            tempMusicList.forEach((elem, index) => {
                if (elem.id == currentMusic.id) {
                    muqian = index;
                    return
                }
            });
            let x = -1;
            do {
                x = parseInt(Math.random() * (0 + tempMusicList.length));
            } while (x == muqian)

            context.commit('nextChangCurrentMusic', x);
            context.dispatch('getSongUrl', tempMusicList[x].id);
            console.log('目前音乐下标', muqian);
        }
    }
}